package xcar_parser

import (
	"crawler/single_crawler/engine"
	"regexp"
)

const host = "http://newcar.xcar.com.cn"

var carModelRe = regexp.MustCompile(`<a href="(/\d+/)" target="_blank" class="list_img">`)
var carListRe = regexp.MustCompile(`<a href="(//newcar.xcar.com.cn/car/[\d+-]+\d+/)"`)

func ParseCarList(contents []byte, _ string) engine.ParseResult {
	result := engine.ParseResult{}

	matches := carModelRe.FindAllSubmatch(contents, -1)
	for _, m := range matches {
		result.Items = append(result.Items, "Car model "+string(m[0]))
		result.Requests = append(result.Requests, engine.Request{
			Url:        host + string(m[1]),
			ParserFunc: ParseCarModel,
		})
	}

	matches = carListRe.FindAllSubmatch(contents, -1)
	for _, m := range matches {
		result.Items = append(result.Items, string(m[0]))
		result.Requests = append(result.Requests, engine.Request{
			Url:        "http:" + string(m[1]),
			ParserFunc: ParseCarList,
		})
	}

	return result
}
